//
//  DishAlbumView.m
//  XRmenu
//
//  Created by Chow Akins on 11-3-20.
//  Copyright 2011 E0571. All rights reserved.
//

#import "DishAlbumView.h"
@interface DishAlbumView(Private)
-(void)adjustUIWithLanguageChange;
@end

#import "NSDictionary+Config.h"

@implementation DishAlbumView
@synthesize mWrapperView;

-(void)dealloc
{
	
	removeNObserver(kMessage_Album_orderedChange);
	removeNObserver(kMessage_Album_clearordered);
	
	self.mWrapperView=nil;
	[super dealloc];
}


#define kTagDishThumbWrapperView 401

#define kAnimationTime 0.4f

#define kTagSortViewBegin 801

-(id) initWithStyle:(NSString *)stylename DishSubViewName:(NSString *)vname dishId:(NSString*)aDishId
{
	self = [super initWithStyle:stylename DishSubViewName:vname];
	if (self) { 
		self.clipsToBounds=NO;
		//NSDictionary* aDic = [self findNode:APPSESSION.allconfig withKey:@"DishThumbWrapperView"];
		DishThumbWrapperView* wrapperView =[[DishThumbWrapperView alloc] initWithTarget:self position:CGPointMake(63, 1) style:DisplayModelWithAll];
		
		wrapperView.frame = CGRectMake(wrapperView.frame.origin.x, wrapperView.frame.origin.y, wrapperView.frame.size.width+50, wrapperView.frame.size.height+3+30);
		wrapperView.tag = kTagDishThumbWrapperView;
		self.mWrapperView = wrapperView;
		[self addSubview:wrapperView];
		[wrapperView release]; 
#ifdef DishTestColor 
		wrapperView.backgroundColor =[UIColor blueColor];
#else
		self.backgroundColor =[UIColor clearColor];
#endif 
		addNObserver(@selector(freshWithChange:),kMessage_Album_orderedChange);
		addNObserver(@selector(cleanOrderd:),kMessage_Album_clearordered);
		[self add1fingerTap];
	}
	return self;
}

-(void)freshWithChange:(NSNotification*)aNotif
{
	//接受外部改变通知---刷新点单数量
	[self.mWrapperView freshInfoWithProduct:aNotif.object];
}

-(void)cleanOrderd:(NSNotification*)aNotif
{

	[self.mWrapperView cleanOrderd];
	
}

#pragma mark 基本方法
-(void)showWithDishId:(NSString*)aDishId
{  
    DishThumbWrapperView* wrapperView = (DishThumbWrapperView*)[self viewWithTag:kTagDishThumbWrapperView];
	[wrapperView showPageAtDishId:aDishId];
}

-(void) handleTapFrom:(UITapGestureRecognizer *)recognizer
{
	CGPoint location = [recognizer locationInView:self];
	
	CGRect f = CGRectMake(0, 0, 155, 34);
	
	if (CGRectContainsPoint(f, location)) {
		postN(kMessage_Main_Clean);
		return;
	}
	
	DishThumbWrapperView* wrapperView = (DishThumbWrapperView*)[self viewWithTag:kTagDishThumbWrapperView];

	if (location.x<63) {
		[wrapperView handleSwipeRight:nil];
	}
	else if (location.x>1024-63) {
		[wrapperView handleSwipeLeft:nil]; 
	}
}

-(void)freshViewWithLanguageChange
{
	
}

-(void)freshViewWithDataChange
{
	
}

-(void)freshViewWithSpecifyId
{
	
}
					 
#pragma mark DishAlbumNavViewDelegate
-(void)dishAlbumNavView:(DishAlbumNavView *)navView perIndex:(NSInteger)perIndex changToIndex:(NSInteger)aIndex
{
	
	return;
	/*FTAnimationDirection
	kFTAnimationTop = 0,
	kFTAnimationRight,1
	kFTAnimationBottom,2
	kFTAnimationLeft,3
	kFTAnimationTopLeft,4
	kFTAnimationTopRight,5
	kFTAnimationBottomLeft,6
	kFTAnimationBottomRight,7
	*/
	if (perIndex<aIndex) {//后翻页 
		//[self.mWrapperView
		[self.mWrapperView slideOutTo:kFTAnimationLeft inView:self duration:kAnimationTime delegate:self startSelector:@selector(slideOutToLeftStart) stopSelector:nil];
	}else {
		[self.mWrapperView slideOutTo:kFTAnimationRight inView:self duration:kAnimationTime delegate:self startSelector:@selector(slideOutToRightStart) stopSelector:nil]; 
	}
}

-(void)slideOutToLeftStart
{
	//重新加载数据再进入--过0.1s后进入下一页的页面

	//[self slideInFromRightEnd];
}
-(void)slideInFromRightEnd
{
	//重新加载数据再进入
	[self.mWrapperView slideInFrom:kFTAnimationRight inView:self duration:kAnimationTime delegate:nil startSelector:nil stopSelector:nil];
}

-(void)slideOutToRightStart
{
	//重新加载数据再进入
}

-(void)slideInFromLeftEnd
{
	//重新加载数据再进入
	[self.mWrapperView slideInFrom:kFTAnimationLeft inView:self duration:kAnimationTime delegate:nil startSelector:nil stopSelector:nil];
}

#pragma mark -
#pragma mark DataSouce
-(NSInteger) dishThumbWrapperViewRowsInPage
{
	return 3;
}
-(NSInteger) dishThumbWrapperViewColumsInPage
{
	return 4;
}
-(NSInteger) dishThumbWrapperDisplayCount
{
	return [APPSESSION.dishdata count];
}

-(DishThumbView*)dishThumbWrapperView:(DishThumbWrapperView *)aDishWrapper viewAtPosition:(NSInteger)aPosition inPageIndex:(NSInteger)aIndex;
{ 
	DishThumbView* dishView = (DishThumbView*)[aDishWrapper viewWithTag:(kTagSortViewBegin+aIndex)];
	[dishView setDislpayMode:DisplayModelWithAll];
	Product* p = [APPSESSION.dishdata objectAtIndex:aPosition];
	[dishView showWithItem:p]; 
	return dishView;
}
@end
